package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.server.service.SettinsService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//通用设置相关的controller
@RestController
@RequestMapping("/users")
public class SettingsController {

//    接口路径：GET/users/settings
    @Autowired
    private SettinsService settinsService;

    @Autowired
    private UserService userService;

    /*
     * 1.请求方式 POST
     * 2.请求地址 /users/phone
     * 3.请求头 Authorization 里面放的是token值
     * 4.请求体 phone
     * 5.返回结果
     * */

    @PostMapping(value = "/phone", name = "修改手机号——保存")
    public ResponseEntity saveNewMobile(@RequestBody Map<String, String> param) {
        String mobile = param.get("phone");
        return userService.saveNewMobile(mobile);
    }

    /*
     * 1.请求方式 POST
     * 2.请求地址 /users/phone/checkVerificationCode
     * 3.请求头 Authorization 里面放的是token值
     * 4.请求体 verificationCode
     * 5.返回结果 verification  boolean 验证是否通过
     * */

    @PostMapping(value = "/phone/checkVerificationCode", name = "验证验证码是否通过")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> param) {
        String code = param.get("verificationCode");
        return userService.checkCode(code);
    }

    /*
     * 1.请求方式 POST
     * 2.请求地址 /users/phone/sendVerificationCode
     * 3.请求头 Authorization 里面放的是token值
     * 4.请求体 wu
     * 5.返回结果 wu
     * */
    @PostMapping(value = "/phone/sendVerificationCode",name = "修改手机号-向原发送验证码")
    public ResponseEntity sendVerificationCode() {
        User user = UserHolder.getUser();
        String phone = user.getMobile();
        return userService.sendCode(phone);
    }



    @PostMapping(value = "/notifications/setting",name = "保存通知设置")
    public ResponseEntity updateNotification(@RequestBody Map map){
        Boolean like = (Boolean) map.get("likeNotification");
        Boolean pinglun = (Boolean) map.get("pinglunNotification");
        Boolean gonggao = (Boolean) map.get("gonggaoNotification");
        return settinsService.updateNotification(like,pinglun,gonggao);
    }

    @PostMapping(value = "/questions",name = "保存陌生人问题")
    public ResponseEntity saveQuestions(@RequestBody Map map){
       String content = (String) map.get("content");
        return settinsService.saveQuestions(content);
    }

    @DeleteMapping(value = "/blacklist/{uid}",name = "取消黑名单")
    public ResponseEntity delBlacklist(@PathVariable("uid") Long blackListId){
       return settinsService.delBlacklist(blackListId);
    }


//    page	 默认值：1
//    当前页数 pagesize	默认值：10
    @GetMapping(value = "/blacklist",name = "获取当前登录人的很名单列表")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        return  settinsService.blacklist(page,pagesize);
    }

    @GetMapping(value = "/settings",name = "获取通用设置")
    public ResponseEntity querySettings(){
        return  settinsService.querySettings();
    }

}
